package com.stylefeng.guns.modular.product.controller;

import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.common.annotion.Permission;
import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.common.constant.dictmap.UserDict;
import com.stylefeng.guns.modular.product.warpper.ProductFxWarpper;
import com.stylefeng.guns.modular.product.service.IExcelUploadService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.product.model.ProductFx;
import com.stylefeng.guns.modular.product.service.IProductFxService;

import java.util.List;
import java.util.Map;

/**
 * 产品管理控制器
 *
 * @author fengshuonan
 * @Date 2019-03-15 13:57:45
 */
@Controller
@RequestMapping("/productFx")
public class ProductFxController extends BaseController {

    private String PREFIX = "/product/productFx/";

    @Autowired
    private IProductFxService productFxService;

    @Autowired
    private GunsProperties gunsProperties;

    @Autowired
    private IExcelUploadService excelUploadService;


    /**
     * 跳转到产品管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "productFx.html";
    }

    /**
     * 跳转到添加产品管理
     */
    @RequestMapping("/productFx_add")
    public String productFxAdd() {
        return PREFIX + "productFx_add.html";
    }

    /**
     * 跳转到修改产品管理
     */
    @RequestMapping("/productFx_update/{productFxId}")
    public String productFxUpdate(@PathVariable Integer productFxId, Model model) {
        ProductFx productFx = productFxService.selectById(productFxId);
        model.addAttribute("item",productFx);
        LogObjectHolder.me().set(productFx);
        return PREFIX + "productFx_edit.html";
    }

    /**
     * 获取产品管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String customer, @RequestParam(required = false) String entryDate, @RequestParam(required = false) String tradeDate) {
        List<Map<String, Object>> list = productFxService.selectProductFX(customer, entryDate, tradeDate);
        return new ProductFxWarpper(list).warp();
        //return productFxService.selectList(null);
    }

    /**
     * 新增产品管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ProductFx productFx) {
        productFxService.insert(productFx);
        return SUCCESS_TIP;
    }

    /**
     * 删除产品管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer productFxId) {
        productFxService.deleteById(productFxId);
        return SUCCESS_TIP;
    }

    /**
     * 修改产品管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ProductFx productFx) {
        productFxService.updateById(productFx);
        return SUCCESS_TIP;
    }

    /**
     * 产品管理详情
     */
    @RequestMapping(value = "/detail/{productFxId}")
    @ResponseBody
    public Object detail(@PathVariable("productFxId") Integer productFxId) {
        return productFxService.selectById(productFxId);
    }

    /**
     * Closed产品FX
     */
    @RequestMapping(value = "/closed")
    @BussinessLog(value = "Closed产品FX", key = "userId", dict = UserDict.class)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object closed(@RequestParam Integer productFxId) {
        productFxService.setPositionStatus(productFxId, Const.CLOSED);
        return SUCCESS_TIP;
    }

    /**
     * Closed产品FX
     */
    @RequestMapping(value = "/open")
    @BussinessLog(value = "Open产品FX", key = "userId", dict = UserDict.class)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object open(@RequestParam Integer productFxId) {
        productFxService.setPositionStatus(productFxId, Const.OPEN);
        return SUCCESS_TIP;
    }

    /**
     * 上传excel

    @RequestMapping(method = RequestMethod.POST, path = "/uploadProduct")
    @ResponseBody
    public String uploadProduct(@RequestPart("file") MultipartFile excelFile) {
        String originalFilename = excelFile.getOriginalFilename();
        String fileSavePath = gunsProperties.getExcelUploadPath();
        try{
            //int a = 1/0;
            excelFile.transferTo(new File(fileSavePath + originalFilename));
            //excelImport(excelFile);
            boolean result = productFxService.parseExcelToBatchInsert(excelFile);
            if(result){
                ExcelUpload excelUpload = new ExcelUpload();
                excelUpload.setFilename(originalFilename);
                excelUpload.setUploadtime(new DateTime());
                excelUploadService.insert(excelUpload);
            }
        }catch (Exception e){
            throw new GunsException(BizExceptionEnum.FILE_READING_ERROR);
        }
        return originalFilename;
    }
     */
}
